package com.huihuiyun.huihuiyun.server.controller.member.client;

import com.huihuiyun.huihuiyun.server.controller.member.client.viewobjects.HhyMemberShoppingCartVO;
import com.huihuiyun.huihuiyun.server.domain.HhyMemberShoppingCart;
import com.huihuiyun.huihuiyun.server.domain.HhyProduct;
import com.huihuiyun.huihuiyun.server.service.HhyMemberShoppingCartService;
import com.huihuiyun.huihuiyun.server.service.HhyProductService;
import com.huihuiyun.huihuiyun.server.viewobject.ResultVO;
import com.huihuiyun.huihuiyun.server.viewobject.ResultVOUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.util.List;

@Component
@Path(Const.context)
@Slf4j
public class MemberClientMemberShoppingCartController {
    @Context
    private HttpServletResponse httpServletResponse;

    @Autowired
    private HhyProductService hhyProductService;

    @Autowired
    private HhyMemberShoppingCartService hhyShoppingCartService;

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/memberShoppingCart/findAllByMemberId")
    public ResultVO<List<HhyMemberShoppingCartVO>> findAllByMemberId(@QueryParam("memberId") String memberId) {
        try {
            List<HhyMemberShoppingCart> hhyMemberShoppingCartList = hhyShoppingCartService.findAllByMemberId(memberId);
            List<HhyMemberShoppingCartVO> hhyOrderVOList = ResultVOUtil.copyProperties(hhyMemberShoppingCartList, HhyMemberShoppingCartVO.class);
            return ResultVOUtil.success(hhyOrderVOList);
        } catch (Exception e) {
            log.error("", e);
            return ResultVOUtil.error();
        }
    }

    @POST
    @Path("/memberShoppingCart/addToShoppingCart")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public ResultVO<HhyMemberShoppingCartVO> addToShoppingCart(HhyMemberShoppingCartVO hhyMemberShoppingCartVO) {
        try {
            String memberId = hhyMemberShoppingCartVO.memberId;
            String productId = hhyMemberShoppingCartVO.productId;
            // 之前是否添加了购物车，获取之前的记录
            HhyMemberShoppingCart hhyMemberShoppingCart = hhyShoppingCartService.findIfHhyMemberShoppingCartByMemberIdAndProductId(memberId, productId);
            // 更新最新的商品信息
            HhyProduct hhyProduct = hhyProductService.findOne(productId);
            hhyMemberShoppingCart.sellerId = hhyProduct.sellerId;
            hhyMemberShoppingCart.productId = hhyProduct.id;
            hhyMemberShoppingCart.productIcon = hhyProduct.icon;
            hhyMemberShoppingCart.productOriginalPrice = hhyProduct.originalPrice;
            hhyMemberShoppingCart.productMemberPrice = hhyProduct.memberPrice;
            hhyMemberShoppingCart.productExtraPoints = hhyProduct.extraPoints;
            // 如果之前添加了购物车，商品数量累加
            hhyMemberShoppingCart.productQuantity = hhyMemberShoppingCart.productQuantity + hhyMemberShoppingCartVO.productQuantity;
            hhyMemberShoppingCart = hhyShoppingCartService.put(hhyMemberShoppingCart);
            hhyMemberShoppingCartVO = ResultVOUtil.convert(hhyMemberShoppingCart, HhyMemberShoppingCartVO.class);
            return ResultVOUtil.success(hhyMemberShoppingCartVO);
        } catch (Exception e) {
            log.error("", e);
            return ResultVOUtil.error("添加购物车失败，请稍后重试.");
        }
    }
}